<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <script src="__STATIC__/js/jquery-3.3.1.js" type="application/javascript"></script>
    <script src="__STATIC__/layui/layui.all.js" type="application/javascript"></script>
    <link href="__STATIC__/layui/css/layui.css" type="text/css" rel="stylesheet">
    <style>
        .layui-input, .layui-textarea{
            width: 90%;
        }
        fieldset{
            margin-top: 19px !important;
        }

    </style>
</head>
<body>
    <fieldset  class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend>题目信息</legend>
    </fieldset>
    <form class="layui-form" action="" id="addForm">
        <div class="layui-form-item">
            <label class="layui-form-label">题目</label>
            <div class="layui-input-block">
                <input type="text" name="subject" placeholder="请输入题目" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">答案选项</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入答案选项" class="layui-textarea" name="optional"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">正确答案</label>
            <div class="layui-input-block">
                <input type="text" name="answer"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">题目分值</label>
            <div class="layui-input-block">
                <input type="text" name="score" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="submitAdd">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</body>
</html>
<script>
    //数据添加
    $("#submitAdd").click(function () {
        var data = $("#addForm").serialize();
        var ca = /^[\w ： \u4e00-\u9fa5 \d ,，]+;[\w ： \u4e00-\u9fa5 \d ,，]+;[\w ：\u4e00-\u9fa5 \d ,，]+;[\w ：\u4e00-\u9fa5 \d ,，]+/;
        var $str = $("[name=optional]").val();
        var an = /^[A-D]$/;
        var $atr = $("[name=answer]").val();

           if(an.test($atr)){
               $.ajax({
                   url:"{:url('Form/addt')}",
                   type:"post",
                   data: data,
                   dataType:'json',
                   success:function(result){
                       if (result == 1) {
                           layer.msg('添加成功',{icon:1,time:2000});
                           refreshParent();
                       }else{
                           layer.msg('添加失败',{icon:1,time:2000});
                       }
                   }
               })
           }else{
               layer.msg('正确答案必须填写大写的英文字母A,B,C,D');
           }
    })
    //关闭父窗口，刷新子窗口
    function refreshParent() {
        window.parent.location.reload();
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
</script>
</body>
</html>